package cn.agiledata.bank.account.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.AccountGroups;
import cn.agiledata.bank.hibernateBean.AccountGroupsHis;
import cn.agiledata.bank.hibernateBean.AcctBalHis;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.TransDetail;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.hibernateBean.TransactionHis;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class AccountHistoryObserver extends IPBHibernate3BaseMgr implements
        AccountHistoryObserverInter {

    /*
     * (non-Javadoc)
     * 
     * @see cn.agiledata.bank.impl.AccountHistoryObserverInter#update(java.lang.Object)
     */
    public void update(Object account, Double amount, Double fee,
            String receiveOrPay, Transaction tran, Long peerAccount) {
        if (account instanceof SubAccount) {
            AcctBalHis abh = new AcctBalHis();
            SubAccount sa = (SubAccount) account;
            abh.setBalance(sa.getAccountBalance().getBalance());
            abh.setLockAmount(sa.getAccountBalance().getLockAmount());
            abh.setSubAcctSeq(sa.getId());
            abh.setUpdateTime(new Date());
            abh.setUpdateUser(sa.getAccountId());
            abh.setAmount(amount.toString());
            abh.setFee(fee);
            abh.setTransid(tran.getTransId());
            abh.setPeerSeq(peerAccount);
            abh.setFlag(receiveOrPay);
            // 新增加两行by johnny
            abh.setBankrollUpdateTime(sa.getAccountBalance()
                    .getBankrollUpdateTime());
            abh.setAccumulation(sa.getAccountBalance().getAccumulation());
            getHibernateTemplate().save(abh);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see cn.agiledata.bank.impl.AccountHistoryObserverInter#updateAll(java.util.List)
     */
    public void updateAll(Object[] detailList) {

        AccountTemplateInter at = (AccountTemplateInter) IPBMgrFactory
                .getMgr("accountTemplate");
        for (int i = 0; i < detailList.length; i++) {
            TransDetail td = (TransDetail) detailList[i];
            if (td.getAccountSeq().longValue() < Long
                    .parseLong(Constant.INITIAL_ACCOUNT_NOASSURE)) {
                this
                        .update(
                                at.getSubAccount(td.getAccountSeq().toString()),
                                td.getAmount(), new Double(0),
                                returnReceiveOrPay(td.getAmount()), td
                                        .getTransaction(), td.getPeerAcctSeq());
            }
        }

    }

    private String returnReceiveOrPay(Double dou) {
        if (dou.doubleValue() < 0) {
            return Constant.PAY;
        } else {
            return Constant.RECEIVE;
        }
    }

    public void updateTranHis(Transaction tran) {
        TransactionHis th = new TransactionHis();
        try {
            BeanUtils.copyProperties(th, tran);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        th.setId(null);
        th.setActionSeq(tran.getActionType().getId());
        th.setCreditSeq(tran.getCredit().getId());
        th.setDebitSeq(tran.getDebit().getId());
        th.setRelating(tran.getRelating());
        th.setOperSource(tran.getOperSource());
        th.setAuditTrans(tran.getAuditTrans());
        // 封装插入日期和修改日期
         th.setBeginDate(new Date());
        WeatherUtil.fillCreateAndUpdateInfo(th, tran.getCreateUser());
        getHibernateTemplate().save(th);
    }

    /***************************************************************************
     * @记录群组修改历史
     */
    public void updateAccountGroupHis(AccountGroups accGroup) {
        AccountGroupsHis ag = new AccountGroupsHis();
        try {
            BeanUtils.copyProperties(ag, accGroup);
        } catch (IllegalAccessException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        ag.setId(null);
        WeatherUtil
                .fillCreateAndUpdateInfo(ag, ag.getAccount().getCreateUser());
        getHibernateTemplate().save(ag);
    }

    public AccountGroups getAccountGroups(String accountId, String groupType) {
        List ls = getHibernateTemplate().find(
                "from AccountGroups a where a.account.id=? and a.groups.groupType=? "
                        + "order by a.updateTime desc",
                new Object[] { new Long(accountId), groupType });
        if (ls.size() > 0) {
            return (AccountGroups) ls.get(0);
        } else {
            return null;
        }
    }

    public List getAccountGroups(String id) {
        return getHibernateTemplate().find(
                "from AccountGroups a where a.groups.id=? ",
                new Object[] { new Long(id) });
    }
}
